package demo;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:恋恋
 * Date:2022-09-16
 * Time:8:28
 */
interface USB{
    void openDevice();
    void closeDevice();
}
//鼠标类，实现USB接口
class Mouse implements USB{

    @Override
    public void openDevice() {
        System.out.println("打开鼠标");

    }

    @Override
    public void closeDevice() {
        System.out.println("关闭鼠标");
    }
    public void click(){
        System.out.println("鼠标点击");
    }
}
//键盘类，实现USB接口
class KeyBoard implements USB{
    @Override
    public void openDevice() {
        System.out.println("打开键盘");
    }

    @Override
    public void closeDevice() {
        System.out.println("关闭鼠标");

    }
    public void inPut(){
        System.out.println("键盘输入");
    }
}
//笔记本类：使用USB设备
class Computer{
    public void power(){
        System.out.println("打开笔记本电脑");
    }
    public void powerOff(){
        System.out.println("关闭笔记本电脑");
    }
    //应用接口的类，其实也是子类
    //向上转型
    public void useDevice(USB usb){
        usb.openDevice();
        //向下转型——instanceof——是为了应用子类特殊的方法
        if(usb instanceof Mouse){
            Mouse mouse=(Mouse) usb;
            mouse.click();
        }else if(usb instanceof KeyBoard){
            KeyBoard keyBoard=(KeyBoard) usb;
            keyBoard.inPut();
        }
        usb.openDevice();
    }
}
public class TestDome {
    public static void main(String[] args) {
        Computer computer=new Computer();
        computer.power();
        computer.useDevice(new Mouse());
        computer.useDevice(new KeyBoard());
        computer.powerOff();
    }

}
